{extend name="index/base" /}
{block name="content"}
<style type="text/css">
    .layui-input-block{
        margin-left: 200px;
    }
    .item{
        margin-left: 0;
    }
    .domain{
        width: 400px;
        margin: 2px 0;
        display: inline;
    }
    .domainChildrenBox{
        display: block;
    }
    .domainChildren{
        width: 260px;
        margin: 2px 2px 0 10px;
        display: inline;
    }
</style>
<div>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="status" id="status_1" value="1" title="开启">
                <input type="radio" name="status" id="status_0" value="0" title="禁用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">SETHOMEPAGE</label>
            <div class="layui-input-block">
                <a href="javascript:;" class="layui-btn" id="sethomepageAddBtn">添加</a>
            </div>
            <div class="layui-input-block" id="sethomepageBox">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">DESKTOPSHORTCUT</label>
            <div class="layui-input-block">
                <a href="javascript:;" class="layui-btn" id="desktopshortcutAddBtn">添加</a>
            </div>
            <div class="layui-input-block" id="desktopshortcutBox">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">IEFAVORITES</label>
            <div class="layui-input-block">
                <a href="javascript:;" class="layui-btn" id="iefavoritesAddBtn">添加</a>
            </div>
            <div class="layui-input-block" id="iefavoritesBox">

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id" value="{$id}">
                <a class="layui-btn" lay-submit lay-filter="submit">提交</a>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="js"}
<script type="text/javascript">
    layui.use(['table','jquery','form','upload'], function(){
        var table = layui.table; var form = layui.form;var upload = layui.upload;
        var $ = layui.$;
        form.on('submit(submit)', function(data){
            console.log(data.field);
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post("{:url('editConfig')}", data.field, function (res) {
                layer.close(loading);
                if (res.code == 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                        window.parent.location.reload();
                        return false;
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });

        var itemIndex = 0;
        var childrenItemIndex = 0;
        $('#sethomepageAddBtn').click(function(){
            var str = '';
            var thisItemIndex = itemIndex;
            str += '<div id="item_'+thisItemIndex+'" class="layui-input-block item"><input type="text" placeholder="域名" class="layui-input domain" name="domain['+thisItemIndex+']"> ';
            str += '<a class="layui-btn layui-btn-xs" href="javascript:;" id="item_'+thisItemIndex+'_addBtn">添加子项</a>';
            str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="item_'+thisItemIndex+'_delBtn">删除</a>';
            str += '<div id="childrenItem_'+thisItemIndex+'"></div>';
            str += '</div>';

            $('#sethomepageBox').append(str);

            $('#item_'+thisItemIndex+'_addBtn').click(function(){
                var str2 = '';
                var thisChildrenItemIndex = childrenItemIndex;
                str2 += '<div><input type="text" placeholder="程序名" name="process['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren">';
                str2 += '<input type="text" placeholder="文件路径" name="path['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren">';
                str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="true" value="true" class="domainChildren">';
                str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="false" value="false" checked class="domainChildren"> ';
                str2 += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="childrenItem_'+thisChildrenItemIndex+'_delBtn">删除</a></div>';
                $('#childrenItem_'+thisItemIndex).append(str2);
                $('#childrenItem_'+thisChildrenItemIndex+'_delBtn').click(function(){
                    $(this).parent().remove();
                })
                form.render('radio');
                childrenItemIndex++;
            });

            $('#item_'+thisItemIndex+'_delBtn').click(function(){
                $(this).parent().remove();
            })
            itemIndex++;
        });

        var desktopshortcutIndex = 0;
        $('#desktopshortcutAddBtn').click(function(){
            var str = '';
            str += '<div class="layui-input-block item"><input type="text" class="layui-input domain" name="desktopshortcut_name[]" placeholder="网站名称"> ';
            str += '<input type="text" class="layui-input domain" name="desktopshortcut_domain[]" placeholder="域名"> ';
            str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="desktopshortcut_'+desktopshortcutIndex+'_delBtn">删除</a>';
            str += '</div>';

            $('#desktopshortcutBox').append(str);

            $('#desktopshortcut_'+desktopshortcutIndex+'_delBtn').click(function(){
                $(this).parent().remove();
            })
            desktopshortcutIndex++;
        });

        var iefavoritesIndex = 0;
        $('#iefavoritesAddBtn').click(function(){
            var str = '';
            str += '<div class="layui-input-block item"><input type="text" class="layui-input domain" name="iefavorites_name[]" placeholder="网站名称"> ';
            str += '<input type="text" class="layui-input domain" name="iefavorites_domain[]" placeholder="域名"> ';
            str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="iefavorites_'+iefavoritesIndex+'_delBtn">删除</a>';
            str += '</div>';

            $('#iefavoritesBox').append(str);
            $('#iefavorites_'+iefavoritesIndex+'_delBtn').click(function(){
                $(this).parent().remove();
            })
            iefavoritesIndex++;
        });

        $.post('{:url("getConfig")}',{id:'{$id}'},function(res){
            if(res.code==0) {
                $('#status_'+res.data.status).attr('checked','checked');
                form.render('radio');
                $.each(res.data.shp,function(index, o){
                    var str = '';
                    var thisItemIndex = itemIndex;
                    str += '<div id="item_'+thisItemIndex+'" class="layui-input-block item"><input type="text" placeholder="域名" class="layui-input domain" name="domain['+thisItemIndex+']" value="'+index+'"> ';
                    str += '<a class="layui-btn layui-btn-xs" href="javascript:;" id="item_'+thisItemIndex+'_addBtn">添加子项</a>';
                    str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="item_'+thisItemIndex+'_delBtn">删除</a>';
                    str += '<div id="childrenItem_'+thisItemIndex+'"></div>';
                    str += '</div>';

                    $('#sethomepageBox').append(str);

                    $('#item_'+thisItemIndex+'_addBtn').click(function(){
                        var str2 = '';
                        var thisChildrenItemIndex = childrenItemIndex;
                        str2 += '<div><input type="text" placeholder="程序名" name="process['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren">';
                        str2 += '<input type="text" placeholder="文件路径" name="path['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren">';
                        str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="true" value="true" class="domainChildren">';
                        str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="false" value="false" checked class="domainChildren"> ';
                        str2 += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="childrenItem_'+thisChildrenItemIndex+'_delBtn">删除</a></div>';
                        $('#childrenItem_'+thisItemIndex).append(str2);
                        $('#childrenItem_'+thisChildrenItemIndex+'_delBtn').click(function(){
                            $(this).parent().remove();
                        })
                        form.render('radio');
                        childrenItemIndex++;
                    });

                    $('#item_'+thisItemIndex+'_delBtn').click(function(){
                        $(this).parent().remove();
                    });

                    $.each(o,function(index2, p){
                        var processVal = '';
                        var pathVal = '';
                        var CHANGEPARENT = '';
                        $.each(p,function(index3, q){
                            if(index3 != 'CHANGEPARENT') {
                                processVal = index3;
                                pathVal = q;
                            } else {
                                CHANGEPARENT = q;
                            }
                        })
                        var str2 = '';
                        var thisChildrenItemIndex = childrenItemIndex;
                        str2 += '<div><input type="text" placeholder="程序名" name="process['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren" value="'+processVal+'">';
                        str2 += '<input type="text" placeholder="文件路径" name="path['+thisItemIndex+']['+thisChildrenItemIndex+']" class="layui-input domainChildren" value="'+pathVal+'">';
                        if(CHANGEPARENT == true) {
                            str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="true" value="true" checked class="domainChildren">';
                            str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="false" value="false" class="domainChildren"> ';
                        } else {
                            str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="true" value="true" class="domainChildren">';
                            str2 += '<input type="radio" name="CHANGEPARENT['+thisItemIndex+']['+thisChildrenItemIndex+']" title="false" value="false" checked class="domainChildren"> ';
                        }
                        
                        str2 += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="childrenItem_'+thisChildrenItemIndex+'_delBtn">删除</a></div>';
                        $('#childrenItem_'+thisItemIndex).append(str2);
                        $('#childrenItem_'+thisChildrenItemIndex+'_delBtn').click(function(){
                            $(this).parent().remove();
                        })
                        form.render('radio');
                        childrenItemIndex++;
                    });
                    itemIndex++;
                });
                $.each(res.data.dtsc, function(index, o) {

                    var str = '';
                    str += '<div class="layui-input-block item"><input type="text" class="layui-input domain" name="desktopshortcut_name[]" placeholder="网站名称" value="'+index+'"> ';
                    str += '<input type="text" class="layui-input domain" name="desktopshortcut_domain[]" placeholder="域名" value="'+o+'"> ';
                    str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="desktopshortcut_'+desktopshortcutIndex+'_delBtn">删除</a>';
                    str += '</div>';

                    $('#desktopshortcutBox').append(str);

                    $('#desktopshortcut_'+desktopshortcutIndex+'_delBtn').click(function(){
                        $(this).parent().remove();
                    })
                    desktopshortcutIndex++;
                });
                $.each(res.data.ief, function(index, o) {
                    var str = '';
                    str += '<div class="layui-input-block item"><input type="text" class="layui-input domain" name="iefavorites_name[]" placeholder="网站名称" value="'+index+'"> ';
                    str += '<input type="text" class="layui-input domain" name="iefavorites_domain[]" placeholder="域名" value="'+o+'"> ';
                    str += '<a class="layui-btn layui-btn-xs layui-btn-danger" id="iefavorites_'+iefavoritesIndex+'_delBtn">删除</a>';
                    str += '</div>';

                    $('#iefavoritesBox').append(str);
                    $('#iefavorites_'+iefavoritesIndex+'_delBtn').click(function(){
                        $(this).parent().remove();
                    })
                    iefavoritesIndex++;
                });
            }
        })
    });
</script>
{/block}